package dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import util.JpaUtil;
import entity.User;
import entity.Watched;

public class WatchedDao {
	EntityManager em = JpaUtil.getEntityManager();

	public void insertWatchedFilm(User user, int movieId) {
		EntityTransaction tx = em.getTransaction();
		tx.begin();
		Watched watched = new Watched(user.getUserId(), movieId);
		em.persist(watched);
		tx.commit();
	}

	public ArrayList<Watched> selectWatchedFilmsByUser(User user) {

		TypedQuery<Watched> query = em.createQuery(
				"SELECT c FROM Watched c WHERE c.userId = ?1", Watched.class);
		query.setParameter(1, user.getUserId());
		List<Watched> results = query.getResultList();

		ArrayList<Watched> resw = new ArrayList<Watched>();
		for (Watched w : results) {
			resw.add(w);
		}
		return resw;
	}
}
